package com.jvm.interpreter.instruction;

import com.jvm.Utils;
import com.jvm.classfile.model.Class;
import com.jvm.classfile.model.Method;
import com.jvm.interpreter.Frame;
import com.jvm.interpreter.Instruction;
import com.jvm.interpreter.MetaSpace;

/**
 * @program: diy_simple_jvm
 * @description:
 * @author: CaoYong
 * @create: 2021-07-09 16:03
 **/
public class InvokeSpecialInst implements Instruction {

    final String clazz;
    final String name;
    final String descriptor;

    InvokeSpecialInst(String clazz, String name, String descriptor) {
        this.clazz = clazz;
        this.name = name;
        this.descriptor = descriptor;
    }

    @Override
    public int offset() {
        return 3;
    }

    @Override
    public void eval(Frame frame) {
        Class cls = MetaSpace.getClassLoader().findClass(clazz);
        Method method = cls.getSpecialStaticMethod(name, descriptor);
        Utils.clinit(method.clazz);
        Utils.invokeMethod(method);
        frame.pc += offset();
    }
}
